package com.billsharing.server;


import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import net.sf.json.JSONObject;
import com.google.gson.Gson;



/**
 * Servlet implementation class HomeResponse
 */
@WebServlet("/HomeResponse")
public class HomeResponse extends HttpServlet 

{
	private static final long serialVersionUID = 1L;
	int uid;
	String json;

 Map<String, Double> creditInfo = new HashMap<String, Double>();
 Map<String, Double> debitInfo = new HashMap<String, Double>();

	   
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		// TODO Auto-generated method stub
		//JSONArray arrayObj=new JSONArray();
		PrintWriter out=response.getWriter();
		response.setContentType("text/x-json;charset=UTF-8");
		//System.out.println(request.getParameter("uid"));
		uid = Integer.parseInt(request.getParameter("uid"));
		System.out.println("User id in home page is"+request.getParameter("uid"));
 User user = new User(uid) ;
System.out.println("User id in home page"+user.getUid());

JSONObject json =new JSONObject();
		creditInfo = user.getCreditInfo();
		JSONObject creditJson = new JSONObject();
		creditJson.putAll(creditInfo);
json.put("credit", creditJson);
		
		debitInfo = user.getDebitInfo();
		JSONObject json1 = new JSONObject();
		json1.putAll(debitInfo);
		System.out.println("JSON" +json1);
		json.put("debit", json1);
			
		System.out.println("JSON" +json);
        
		out.print(json);

	}

}
